// Ejecucion:
// ./UdpStdClient host puerto
// ./UdpStdClient 127.0.0.1 7755

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void error(const char *);

int main(int argc, char *argv[])
{
    printf("Cliente STL UDP, parámetros : %d\n", argc);
    printf("--------------------------------\n");
//    printf("cadena : %s\n", argv[3]);
    int sock, n;
    unsigned int length;
    struct sockaddr_in server;//, from;
    struct hostent *hp;

    /*
    char str[100] = "ca va Ronald...\n";
    printf(str);
    strcpy(str, "Hola mundo...\n");
    printf(str);
    char test[5]= "abcdefghi";
    printf("str = %s , strlen = %d \n", test, strlen(test));
    */


    const int szBuf = 512;

    char buf[szBuf];
    strcpy(buf, argv[3]);
    printf("size = %d\n", strlen(buf));
    printf("buf = %s\n", buf);

//    char buffer[256] = "UdPbC \s:WI0003,n:123*20\$WRFCS,F,012,VARAYOC2,012 31.05S,078 15.05W,0,3000,0,350,A10,000000,1,5,A0A0A4,1*52\r\n$WRFCS,F,013,VARAYOC2,012 31.05S,078 15.05W,0,3000,0,350,A10,000000,1,5,A0A0A4,1*53\r\n";
//    char buffer[256] = "$--ESM,ID,UND,LID,STT,LAT,LON,ANG,F,AF,Pr,PW,PRF,APRF,PRA,DT*hh<cr><lf>\n\0";
//    char buffer0[256] = "$WRESM,011,VARAYOC2,11110,Q,000 00.00N,111 11.11W,87.9,1000,0,-10.0,0.10,100.0,3,60,20120612165730.5*5F\r\n";
//    char buffer0[256] = "$WRESM,011,VARAYOC2,11110,Q,000 00.00N,111 11.11W,89.0,1000,0,-10.0,0.10,100.0,3,60,20120612165730.5*58\r\n";
    // ultimo campo de la trama anterior "AA" es un checksum calculado en función de todos los campos anteriores del mensaje
    // para este ejemplo esta hardcode, esta pendiente la generacion de la funcion checksum de la trama

//    $--FCS,R,ID,UND,LAT,LON,RINI,RFIN,AINI,AFIN,NAM,COL,WTH,LIN,2COL,STY*hh<cr><lf>
//    char buffer1[256] = "$WRFCS,F,012,VARAYOC2,012 31.05S,078 15.05W,0,3000,0,350,A10,000000,1,5,A0A0A4,1*52\r\n";
//    char buffer3[256] = "$WRFCS,F,013,VARAYOC2,012 31.05S,078 15.05W,0,3000,0,350,A10,000000,1,5,A0A0A4,1*53\r\n";
//    char buffer4[256] = "$WRFCS,F,014,VARAYOC2,012 31.05S,078 15.05W,0,3000,0,350,A10,000000,1,5,A0A0A4,1*54\r\n";

    /*
        $--FPS, R,  ID, UND,    C,  NAME,   COL,    WTH,    LIN,    N,  LATi,   LONi,   2COL,   STY *hh<cr><lf>
        0       1   2   3       4   5       6       7       8       9   10      11      12      13   14
    */
//    char buffer2[256] = "$WRFPS,F,012,VARAYOC2,C,A10,000000,1,5,3,012 32.05S,078 15.05W,011 31.05S,077 15.05W,013 31.05S,079 15.05W,A0A0A4,1*0B\r\n";


    if (argc != 4)
    {
        printf("Parametros: Servidor Puerto\n");
        exit(1);
    }

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) error("Error de definicion de socket");

    server.sin_family = AF_INET;

    hp = gethostbyname(argv[1]);
    if (hp == 0) error("Host desconocido");

    bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
    server.sin_port = htons(atoi(argv[2]));
    length = sizeof(struct sockaddr_in);

//    printf("Mensaje: ");
//    bzero(buffer,256);
//    fgets(buffer,255,stdin);

    n = sendto(sock,buf, strlen(buf),0,(const struct sockaddr *)&server,length);
    if (n < 0) error("Error de Tx");
//    n = sendto(sock,buffer3, strlen(buffer3),0,(const struct sockaddr *)&server,length);
//    if (n < 0) error("Error de Tx");
//    n = sendto(sock,buffer4, strlen(buffer4),0,(const struct sockaddr *)&server,length);
//    if (n < 0) error("Error de Tx");

//    n = recvfrom(sock,buffer0,256,0,(struct sockaddr *)&from, &length);
//    if (n < 0) error("Error de Rx");

//    write(1, "Recepcionado: ", 14);
//    write(1, buffer0, n);

//    n = sendto(sock,buffer1, strlen(buffer1),0,(const struct sockaddr *)&server,length);
//    if (n < 0) error("Error de Tx");

//    n = recvfrom(sock,buffer1,256,0,(struct sockaddr *)&from, &length);
//    if (n < 0) error("Error de Rx");

//    write(1, "Recepcionado: ", 14);
//    write(1, buffer1, n);

//    n = sendto(sock,buffer2, strlen(buffer2),0,(const struct sockaddr *)&server,length);
//    if (n < 0) error("Error de Tx");

//    n = recvfrom(sock,buffer2,256,0,(struct sockaddr *)&from, &length);
//    if (n < 0) error("Error de Rx");

//    write(1, "Recepcionado: ", 14);
//    write(1, buffer2, n);
    /*
*/

    close(sock);
    return 0;
}

void error(const char *msg)
{
    perror(msg);
    exit(0);
}
